//
//  WaveView.m
//  YSSmartHome
//
//  Created by yanghao on 2017/3/12.
//  Copyright © 2017年 yahaong. All rights reserved.
//

#import "WaveView.h"
#import "YHMarcros.h"
#import "CommonUtils.h"
#define BackGroundColor [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1]
#define WaveColor1 [UIColor colorWithRed:221/255.0 green:237/255.0 blue:248/255.0 alpha:1]
#define WaveColor2 [UIColor colorWithRed:187/255.0f green:221/255.0f blue:246/255.0f alpha:1]

@interface WaveView ()
@property (nonatomic, strong) CAShapeLayer* waveLayer1;
@property (nonatomic, strong) CAShapeLayer* waveLayer2;
@property (nonatomic, assign) CGFloat waveMaxAmplitude;
@property (nonatomic, assign) CGFloat wavePhase;
@property (nonatomic, assign) CGFloat waveAngularVelocity;
@property (nonatomic, assign) CGFloat waveX;
@property (nonatomic, assign) CGFloat waveY;
@property (nonatomic, assign) CGFloat waveMoveSpeed;
@property (nonatomic, strong) CADisplayLink* disPlayLink;
@property (nonatomic, strong) UILabel* mLblAirRange;
@property (nonatomic, strong) UIButton* mBtnPM;

@property (nonatomic, strong) UILabel* mLblPMData;
@end
@implementation WaveView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
//        [self configData];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.layer.cornerRadius = self.bounds.size.width/2.0f;
    self.layer.masksToBounds = true;
    [self configData];
}
- (void)setupUI
{
    //初始化波浪
    //底层
    _waveLayer1 = [CAShapeLayer layer];
    _waveLayer1.fillColor = WaveColor1.CGColor;
    _waveLayer1.strokeColor = WaveColor1.CGColor;
    [self.layer addSublayer:_waveLayer1];
    
    //上层
    _waveLayer2 = [CAShapeLayer layer];
    _waveLayer2.fillColor = WaveColor2.CGColor;
    _waveLayer2.strokeColor = WaveColor2.CGColor;
    [self.layer addSublayer:_waveLayer2];
    
    //画了个圆
//    self.layer.cornerRadius = self.bounds.size.width/2.0f;
//    self.layer.masksToBounds = true;
    self.backgroundColor = BackGroundColor;
    
    [self addSubview:self.mLblAirRange];
    [self.mLblAirRange mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(kIphoneSize_Height(45));
    }];
    
    [self addSubview:self.mBtnPM];
    [self.mBtnPM mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.mLblAirRange.bottom).offset(kIphoneSize_Height(30));
    }];
    
    [self addSubview:self.mLblPMData];
    [self.mLblPMData mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.mBtnPM.bottom).offset(kIphoneSize_Height(5));
    }];
    
}
-(void)configData
{
    //振幅
    _waveMaxAmplitude = 10;
    //角速度
    _waveAngularVelocity = M_PI/self.bounds.size.width;
    //偏距
    _waveY = self.bounds.size.height;
    //初相
    _waveX = 0;
    //x轴移动速度
    _waveMoveSpeed = _waveAngularVelocity * 10;
    //以屏幕刷新速度为周期刷新曲线的位置

    [self stop];
    _disPlayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateWave:)];
    [_disPlayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    
}


-(void)updateWave:(CADisplayLink *)link
{
    _waveX += _waveMoveSpeed;
    [self updateWaveY];//更新波浪的高度位置
    [self updateWave];
    
}
//更新偏距的大小 直到达到目标偏距 让wave有一个匀速增长的效果
-(void)updateWaveY
{
    CGFloat targetY = self.bounds.size.height - _progress * self.bounds.size.height;
    if (_waveY < targetY) {
        _waveY += 2;
    }
    if (_waveY > targetY ) {
        _waveY -= 2;
    }
}
-(void)updateWave
{
    //波浪宽度
    CGFloat waterWaveWidth = self.bounds.size.width;
    //初始化运动路径
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGMutablePathRef maskPath = CGPathCreateMutable();
    //设置起始位置
    CGPathMoveToPoint(path, nil, 0, _waveY);
    
    //设置起始位置
    CGPathMoveToPoint(maskPath, nil, 0, _waveY);
    //初始化波浪其实Y为偏距
    CGFloat y = _waveY;
    
    //正弦曲线公式为： y=Asin(ωx+φ)+k;
    for (float x = 0.0f; x <= waterWaveWidth ; x++) {
        y = _waveMaxAmplitude * sin(_waveAngularVelocity * x + _waveX) + _waveY;
        
        CGPathAddLineToPoint(path, nil, x, y);
        
    }
    for (float x = 0.0f; x <= waterWaveWidth ; x++) {
        y = _waveMaxAmplitude * cos(_waveAngularVelocity * x + _waveX) + _waveY;
        
        CGPathAddLineToPoint(maskPath, nil, x, y);
    }
    [self updateLayer:_waveLayer1 path:path];
    [self updateLayer:_waveLayer2 path:maskPath];
    
}
-(void)updateLayer:(CAShapeLayer *)layer path:(CGMutablePathRef )path
{
    //填充底部颜色
    CGFloat waterWaveWidth = self.bounds.size.width;
    CGPathAddLineToPoint(path, nil, waterWaveWidth, self.bounds.size.height);
    CGPathAddLineToPoint(path, nil, 0, self.bounds.size.height);
    CGPathCloseSubpath(path);
    layer.path = path;
    CGPathRelease(path);
}
-(void)setProgress:(CGFloat)progress
{
    [self stop];
    _progress = progress;
    
}
- (void)setValue:(NSInteger)value
{
    _value = value;
    [self setValueStr:value];
    _progress = value / 500.0;
    NSString* dataStr = [NSString stringWithFormat:@"%zd",value];
    NSMutableAttributedString* valueAttributedStr  =[[NSMutableAttributedString alloc]initWithString:dataStr attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:35],NSForegroundColorAttributeName:RGBColor(51, 51, 51)}];
    
    NSAttributedString* unitAttributedStr  =[[NSAttributedString alloc]initWithString:@"  ug/㎥" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:RGBColor(51, 51, 51)}];
    [valueAttributedStr appendAttributedString:unitAttributedStr];
    _mLblPMData.attributedText = valueAttributedStr;
}

- (void)setValueStr:(NSInteger)value
{
    int colorHex = [CommonUtils evaluateColor:0xFF006fc3 endValue:0xFFef4b3a fraction:value / 500.0];
    self.mLblAirRange.textColor = UIColorFromHex(colorHex);
    
    if (value <= 100) {
        self.mLblAirRange.text = @"空气优良";
    }
    else if (100 < value&& value <=200)
    {
        self.mLblAirRange.text = @"轻度污染";
    }
    else if (200 < value&& value <=300)
    {
        self.mLblAirRange.text = @"中度污染";
    }

    else if (300 < value&& value <=400)
    {
        self.mLblAirRange.text = @"中度重污染";
    }
    else if (400 < value)
    {
        self.mLblAirRange.text = @"重度污染";
    }
}


-(void)stop
{
    if (_disPlayLink) {
        [_disPlayLink invalidate];
        _disPlayLink = nil;
    }
}
-(void)dealloc
{
    [self stop];
    if (_waveLayer1) {
        [_waveLayer1 removeFromSuperlayer];
        _waveLayer1 = nil;
    }
    if (_waveLayer2) {
        [_waveLayer2 removeFromSuperlayer];
        _waveLayer2 = nil;
    }
    
}

- (UILabel *)mLblAirRange
{
    if (!_mLblAirRange) {
        _mLblAirRange = [[UILabel alloc]init];
//        _mLblAirRange.text = @"重度污染";
        _mLblAirRange.font = [UIFont systemFontOfSize:16];
        _mLblAirRange.textColor = kGlobalRedColor;
    }
    return _mLblAirRange;
}

- (UIButton *)mBtnPM
{
    if (!_mBtnPM) {
        _mBtnPM = [UIButton buttonWithType:UIButtonTypeCustom];
        [_mBtnPM setBackgroundImage:[UIImage imageNamed:@"pmbackground"] forState:UIControlStateNormal];
        [_mBtnPM setTitle:@"PM2.5" forState:UIControlStateNormal];
        _mBtnPM.titleLabel.font = [UIFont systemFontOfSize:12];
        [_mBtnPM setTitleColor:RGBColor(51, 51, 51) forState:UIControlStateNormal];
    }
    return _mBtnPM;
}

- (UILabel *)mLblPMData
{
    if (!_mLblPMData) {
        _mLblPMData = [[UILabel alloc]init];
            }
    return _mLblPMData;
}
@end
